package com.zhiyizhen.handheld.component.pharmacy.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;

import com.zhiyizhen.handheld.R;
import com.zhiyizhen.handheld.common.DataCache;
import com.zhiyizhen.handheld.common.DateUtil;
import com.zhiyizhen.handheld.common.ImageLoader;
import com.zhiyizhen.handheld.common.ResourcesUtil;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseAdapter;
import com.zhiyizhen.handheld.common.view.adapter.recyclerview.BaseViewHolder;
import com.zhiyizhen.handheld.component.main.bean.EnumFieldBean;
import com.zhiyizhen.handheld.component.pharmacy.bean.RegimenDosageUserBean;
import com.zhiyizhen.handheld.databinding.ItemRegimenDosageBinding;

import java.util.List;

/**
 *
 */
public class RegimenListAdapter extends BaseAdapter<RegimenDosageUserBean, RegimenListAdapter.RegimenDosageViewHolder> {
    private final Context context;

    private int selectPosition = 0;

    public RegimenListAdapter(Context context) {
        super(context);
        this.context = context;
    }

    public void setSelectPosition(int selectPosition) {
        this.selectPosition = selectPosition;
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RegimenDosageViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new RegimenDosageViewHolder(this, ItemRegimenDosageBinding.inflate(LayoutInflater.from(parent.getContext())));
    }

    class RegimenDosageViewHolder extends BaseViewHolder<RegimenDosageUserBean> {

        private final ItemRegimenDosageBinding binding;

        public RegimenDosageViewHolder(BaseAdapter<RegimenDosageUserBean, RegimenDosageViewHolder> adapter, ItemRegimenDosageBinding viewBinding) {
            super(adapter, viewBinding.getRoot());
            binding = viewBinding;
        }

        @Override
        public void bindData(RegimenDosageUserBean data, int position) {
            binding.userNameTv.setText(data.getPatientName());
            binding.numberTv.setText(data.getVisitNo());
            if ("1".equals(data.getPatientGender())) {
                ImageLoader.load(context, R.drawable.icon_man, binding.genderImg);
            } else if ("2".equals(data.getPatientGender())) {
                ImageLoader.load(context, R.drawable.icon_woman, binding.genderImg);
            } else {
                ImageLoader.load(context, R.drawable.icon_unknow, binding.genderImg);
            }

            if ("1".equals(data.getDispenseStatus())) {
                binding.dotImg.setVisibility(View.VISIBLE);
                binding.statusTv.setTextColor(ResourcesUtil.getColor(R.color.black_e6));
            } else {
                binding.dotImg.setVisibility(View.INVISIBLE);
                binding.statusTv.setTextColor(ResourcesUtil.getColor(R.color.black_66));
            }

            binding.timeTv.setText(DateUtil.convertDate(data.getDiagnosisTime()));

            if (selectPosition == position) {
                binding.contentLayout.setBackground(ResourcesUtil.getDrawable(R.drawable.bg_execute_select));
            } else {
                binding.contentLayout.setBackgroundColor(ResourcesUtil.getColor(R.color.transparent));
            }
            binding.contentLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

            List<EnumFieldBean> dispenseStatusEnum = DataCache.getEnumsBean().getEnums().getDispenseStatusEnum();
            if (dispenseStatusEnum != null) {
                for (EnumFieldBean execStatus : dispenseStatusEnum) {
                    if (execStatus.getValue().equals(data.getDispenseStatus())) {
                        binding.statusTv.setText(execStatus.getText());
                        break;
                    }
                    binding.statusTv.setText("");
                }
            } else {
                binding.statusTv.setText("");
            }

        }
    }
}
